<template>
	<div>
		<el-card class="box-card">
			<div class="Serach_b">
				<el-input type="input" autosize placeholder="请输入订单编号" v-model="fy.orderNo"
					@keyup.enter.native="seachEnter" style="width: 300px;margin-left: 20px;">
				</el-input>
				<el-select placeholder="请选择订单状态" v-model="fy.isNotDo" clearable>
					<el-option label="未处理" :value="1"></el-option>
				</el-select>
				<el-button type="primary" icon="el-icon-search" @click="SearchData()">搜索</el-button>
			</div>
			<el-table :data="OrderRsultList" border>
				<el-table-column prop="orderNo" label="订单编号" />
				<el-table-column prop="orderDetailId" label="订单详情编号" />
				<el-table-column label="退款原因">
					<template slot-scope="scope">
						<span v-if="scope.row.refundCauseId == 1">收到商品破损</span>
						<span v-if="scope.row.refundCauseId == 2">商品错发、漏发</span>
						<span v-if="scope.row.refundCauseId == 3">收到商品与描述不符</span>
						<span v-if="scope.row.refundCauseId == 4">商品质量问题</span>
						<span v-if="scope.row.refundCauseId == 5">未按约定时间发货</span>
						<span v-if="scope.row.refundCauseId == 6">买错了</span>
					</template>
				</el-table-column>
				<el-table-column prop="backMoney" label="用户退款金额" />
				<el-table-column label="财务审核状态">
					<template slot-scope="scope">
						<span v-if="scope.row.financeStatus == 0">未审核</span>
						<span v-if="scope.row.financeStatus == 1">审核通过</span>
						<span v-if="scope.row.financeStatus == 2">审核未通过</span>
					</template>
				</el-table-column>
				<el-table-column prop="financeRemark" label="财务退款备注" />
				<el-table-column prop="financeTime" label="财务审核时间" />
				<el-table-column label="管理员审核状态">
					<template slot-scope="scope">
						<span v-if="scope.row.refundStatus == 0">未审核</span>
						<span v-if="scope.row.refundStatus == 1">审核通过</span>
						<span v-if="scope.row.refundStatus == 2">审核未通过</span>
					</template>
				</el-table-column>
				<el-table-column prop="refundRemark" label="管理员退款备注" />
				<el-table-column prop="refundTime" label="管理员审核时间" />
				<el-table-column prop="userApplyTime" label="财务退款时间" />
				<el-table-column prop="backScore" label="待返回鸡的数量" />
				<el-table-column prop="createTime" label="创建时间" />
				<el-table-column prop="financeId" label="财务审核ID" />
				<el-table-column prop="refundId" label="管理员审核ID" />
				<el-table-column label="操作" width="100px">
					<template slot-scope="scope">
						<el-button type="warning" v-if="scope.row.financeStatus === 0" size="mini"
							@click="CWSHClick(scope.row)">财务审核</el-button>
						<div v-if="scope.row.financeStatus === 1">
							<el-button type="warning" v-if="scope.row.refundStatus === 0" size="mini"
								@click="GLYSHClick(scope.row)">管理员审核</el-button>
						</div>
						<div v-if="scope.row.financeStatus === 1">
							<div v-if="scope.row.refundStatus === 1">
								<el-button type="warning" v-if="scope.row.refundConfirmation === 0" size="mini"
									@click="QRTK(scope.row)">确认退款</el-button>
							</div>
						</div>
					</template>
				</el-table-column>
			</el-table>
			<el-dialog title="财务审核" :visible.sync="dialogVisible2" width="30%" :before-close="handleClose">
				<el-input placeholder="请输入财务审核意见" v-model="input" clearable>
				</el-input>
				<span slot="footer" class="dialog-footer">
					<el-button @click="cwSH(2)">驳回</el-button>
					<el-button type="primary" @click="cwSH(1)">确 定</el-button>
				</span>
			</el-dialog>
			<el-dialog title="管理员审核" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
				<el-input placeholder="请输入管理员审核意见" v-model="input" clearable>
				</el-input>
				<span slot="footer" class="dialog-footer">
					<el-button @click="glySH(2)">驳回</el-button>
					<el-button type="primary" @click="glySH(1)">确 定</el-button>
				</span>
			</el-dialog>
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 15]"
				:page-size="fy.size" layout="total, sizes, prev, pager, next, jumper" :total="total">
			</el-pagination>
		</el-card>
	</div>
</template>

<script>
	import * as ShOrderApi from "@/api/PublicSearch";
	import * as OrderRsultApi from "@/api/Search";
	import {
		timestampToTime
	} from "@/utils/date";
	export default {
		data() {
			return {
				fy: {
					size: 10,
					current: 1,
				},
				total: 0,
				OrderRsultList: [],
				timestampToTime,
				OrderId: "",
				dialogVisible2: false,
				dialogVisible: false,
				input: "",
			};
		},
		methods: {
			dateArrayTransfer(dateArray) {
				if (dateArray == null || dateArray == '') {
					return '无';
				}
				if (dateArray.length == 6) {
					var returnDate = dateArray[0] + "-" +
						this.returnDoubleNum(dateArray[1]) + "-" +
						this.returnDoubleNum(dateArray[2]) + " " +
						this.returnDoubleNum(dateArray[3]) + ":" +
						this.returnDoubleNum(dateArray[4]) + ":" +
						this.returnDoubleNum(dateArray[5]);
				} else {
					var returnDate = dateArray[0] + "-" +
						this.returnDoubleNum(dateArray[1]) + "-" +
						this.returnDoubleNum(dateArray[2]) + " " +
						this.returnDoubleNum(dateArray[3]) + ":" +
						this.returnDoubleNum(dateArray[4]) + ":00";
				}
				return returnDate;
			},
			returnDoubleNum(number) {
				return (Array(2).join(0) + number).slice(-2); //创建一个长度为2的数组，且默认用0填充；然后用传过来的数添加都右边，然后从右向左截取两位！
			},
			cwSH(value) {
				ShOrderApi.CWTKsh({
					id: this.OrderId,
					Remark: this.input,
					Status: value,
					token: this.$store.getters.token
				}).then((res) => {
					if (res.data == true) {
						this.$notify({
							title: "成功",
							message: "审核成功",
							type: "success",
						});
						this.SearchData();
						this.dialogVisible2 = false;
					} else {
						this.dialogVisible2 = false;
						this.SearchData();
						this.$notify({
							title: "失败",
							message: "审核失败",
							type: "error",
						});
					}
				});
			},
			glySH(value) {
				ShOrderApi.GLYTKsh({
					id: this.OrderId,
					Remark: this.input,
					Status: value,
					token: this.$store.getters.token
				}).then((res) => {
					if (res.data == true) {
						this.dialogVisible = false;
						this.SearchData();
						this.$notify({
							title: "成功",
							message: "审核成功",
							type: "success",
						});
						this.SearchData();
						this.dialogVisible = false;
					} else {
						this.dialogVisible = false;
						this.SearchData();
						this.$notify({
							title: "失败",
							message: "审核失败",
							type: "error",
						});
					}
				});
			},
			CWSHClick(row) {
				this.dialogVisible2 = true;
				this.input = "";
				this.OrderId = row.id;
			},
			GLYSHClick(row) {
				this.dialogVisible = true;
				this.input = "";
				this.OrderId = row.id;
			},
			QRTK(row) {
				this.$confirm("确认退款？")
					.then((_) => {
						ShOrderApi.QRTK({
							id: row.id,
							orderDetailId: row.orderDetailId,
							orderNo: row.orderNo,
							backMoney: row.backMoney,
							financeStatus: row.financeStatus,
							refundStatus: row.refundStatus,
						}).then((res) => {
							if (res.data == true) {
								this.SearchData();
								this.$notify({
									title: "成功",
									message: res.message,
									type: "success",
								});
							} else {
								this.dialogVisible = false;
								this.SearchData();
								this.$notify({
									title: "失败",
									message: res.message,
									type: "error",
								});
							}
						});
						done();
					})
					.catch((_) => {});
			},
			SearchData() {
				OrderRsultApi.InsertOrderResults(this.fy).then((res) => {
					this.total = res.data.total;
					this.OrderRsultList = res.data.records;
					this.OrderRsultList.map(item => {
						item.createTime = this.dateArrayTransfer(item.createTime)
						item.financeTime = this.dateArrayTransfer(item.financeTime)
						item.refundTime = this.dateArrayTransfer(item.refundTime)
						item.updateTime = this.dateArrayTransfer(item.updateTime)
						item.userApplyTime = this.dateArrayTransfer(item.userApplyTime)
					})
				});
			},
			handleSizeChange(value) {
				this.fy.size = value;
				this.SearchData();
			},
			handleCurrentChange(value) {
				this.fy.current = value;
				this.SearchData();
			},
			handleClose(done) {
				this.$confirm("确认关闭？")
					.then((_) => {
						done();
					})
					.catch((_) => {});
			},
		},
		created() {
			this.SearchData();
		},
	};
</script>

<style lang="scss" scoped>
	.el-pagination {
		margin-top: 10px;
	}

	.box-card {
		width: 95%;
		margin: auto;
	}

	.Serach_b {
		display: flex;
		align-items: center;
		margin-bottom: 20px;
	}

	.Serach_b .el-button {
		margin-left: 20px;
	}

	.el-form-item {
		width: 150px;
	}

	.el-select {
		margin-left: 20px;
	}
</style>
